package com.chengh.vhr.service.serviceImpl;

import com.chengh.vhr.common.myEnum.ErrorCodeEnum;
import com.chengh.vhr.exception.MyException;
import com.chengh.vhr.mapper.EmployeeMapper;
import com.chengh.vhr.model.Employee;
import com.chengh.vhr.model.PageRequest;
import com.chengh.vhr.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;


    SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
    SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
    DecimalFormat decimalFormat = new DecimalFormat("##.00");

    @Override
    public PageInfo<Employee> getPageEmployee(PageRequest pageRequest, String keyword) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());

        return new PageInfo<>(employeeMapper.selectEmployees(keyword));
    }

    /**
     * 添加用户资料
     * @Author chengh
     * @param employee
     * @Date 2021/1/31 20:30
     * @Return int
     */
    @Override
    public int addEmployee(Employee employee) {
        // 计算合同到期时间
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        double month = (Double.parseDouble(yearFormat.format(endContract)) - Double.parseDouble(yearFormat.format(beginContract))) * 12 + (Double.parseDouble(monthFormat.format(endContract)) - Double.parseDouble(monthFormat.format(beginContract)));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(month/12)));
        return employeeMapper.insertSelective(employee);
    }

    /***
     * @Description 获得自增长工号,去数据库查询最大的加1
     * @return java.lang.Integer 
     * @Author chengh
     * @Datetime 2021/1/28 20:35
     */
    @Override
    public Integer getMaxWordID() {
        return employeeMapper.getMaxWordID();
    }

    /**
     * 删除员工
     * @Author chengh
     * @param ids id集合
     * @Date 2021/1/30 14:58
     * @Return int
     */
    @Override
    public int deleteEmps(List<Integer> ids) {
        // 此处需要先判断是否有外键关联.
        List<Employee> employees = employeeMapper.selectWhetherDelete(ids);
        if(employees != null && employees.size() > 0) {
            StringBuffer message = new StringBuffer();
            message.append("所选中的员工");
            for (Employee employee : employees) {
                message.append("【" + employee.getName() + "】");
            }
            message.append("资料有外键关联,删除失败!");
            throw new MyException(message.toString(), ErrorCodeEnum.CUSTOM_EXCEPTION_103.getCode());
        }
        return employeeMapper.deleteEmpsByIds(ids);
    }

    /**
     * 更新员工资料信息
     * @Author chengh
     * @param employee
     * @Date 2021/1/30 16:16
     * @Return int
     */
    @Override
    public int updateEmp(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }


}
